/*************************************************
 * 
 * Copyright by Bruno Eberhard
 * 
 * Created on 02.01.2006
 *
 *************************************************/
package fightplan.persistence;

import static fightplan.persistence.ImportHelper.getString;

import java.io.File;
import java.io.FileInputStream;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import fightplan.business.Dojo;
import fightplan.business.Fighter;
import fightplan.business.Gender;
import fightplan.business.Tournament;

public class ExcelSkrKinderImport2012 {

	static void imprtFightersAndTeams(Tournament tournament, File file) throws Exception {
		POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
		HSSFWorkbook wb = new HSSFWorkbook(fs);

		HSSFSheet sheet = wb.getSheetAt(0);

		// ////////////////////////////////////////////////

		// Dojo
		String dojoName = getString(sheet.getRow(3), 2);
		Dojo dojo = tournament.getOrCreateDojo(dojoName);

		// read Fighters
		readFighters(tournament, dojo, sheet);

		// read Teams
		readTeams(tournament, dojo, sheet);
	}

	private static void readFighters(Tournament tournament, Dojo dojo, HSSFSheet sheet) {
		for (int i = 15; i < 166; i++) {
			HSSFRow row = sheet.getRow(i);
			if (row == null)
				continue;
			String fighterName = getString(row, 1);
			if (fighterName == null || fighterName.length() == 0)
				continue;

			fighterName = ImportHelper.normalizeName(fighterName);

			Fighter fighter = new Fighter();
			fighter.setName(fighterName);
			fighter.setDojo(dojo);

			int c = 4; // column

			try {
				fighter.setYearFormatted(getString(row, c++));
			} catch (IllegalArgumentException iae) {
				// silent
			}

			String kyuString = getString(row, c++);
			if (kyuString != null && kyuString.length() > 0 && Character.isDigit(kyuString.charAt(0))) {
				fighter.setBelt(-Integer.parseInt(kyuString.substring(0, 1)));
			}

			String weightString = getString(row, c++);
			if (weightString != null) {
				try {
					fighter.setWeightFormatted(weightString);
				} catch (IllegalArgumentException iae) {
					// silent
				}
			}

			//

			ExcelSkrImport2011.readKataColumn(tournament, fighter, row, c++, true, Gender.D); // 7
			ExcelSkrImport2011.readKataColumn(tournament, fighter, row, c++, true, Gender.H);

			ExcelSkrImport2011.readIpponColumn(tournament, fighter, row, c++, 10, 19, Gender.D); // 9
			ExcelSkrImport2011.readIpponColumn(tournament, fighter, row, c++, 10, 19, Gender.H);
			ExcelSkrImport2011.readIpponColumn(tournament, fighter, row, c++, 20, 21, Gender.D);
			ExcelSkrImport2011.readIpponColumn(tournament, fighter, row, c++, 20, 21, Gender.H);

			tournament.getFighters().add(fighter);
		}
	}
	
	private static void readTeams(Tournament tournament, Dojo dojo, HSSFSheet sheet) {
		// row, column, startCategory, categoryCount
		ExcelSkrImport2011.readTeams(tournament, dojo, sheet, 4, 12, 22, 4); // kata
	}

}
